package com.coustemview.qianfeng.huaqiangbeistore.fragment;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.Shopcar_listview_adapter;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.voersear_listview_adapter;
import com.coustemview.qianfeng.huaqiangbeistore.model.Shopcar_list_handler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingma on 2016/8/29.
 */
public class Fragment_shopcar extends Fragment {
    private ListView mListView;
    private TextView textall_shopcar;
    private CheckBox mCheckBoxall;
    private Shopcar_listview_adapter mAdapter;
    private Button button_shopcar_settle;
    private List<Shopcar_list_handler> listdata;
    private voersear_listview_adapter adapter;
    private View view1;
    private CheckBox checkbox_shopcar_item;
    private Button buttondecline_shopcar_item, buttonadd_shopcar_item, buttondelete_shopcar_item;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shopcar, null);
        view1 = inflater.inflate(R.layout.shopcar_listview_item, null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
//        mCheckBoxall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                switch (buttonView.getId()) {
//                    case R.id.checkbox_shopcar_checkall:
//                        for (int i = 0; i < listdata.size(); i++) {
//                            listdata.get(i).ischecked = isChecked;
//                        }
//                        mAdapter.notifyDataSetChanged();
//                        break;
////                    case R.id.checkbox_shopcar_item:
////                        Integer position = (Integer) buttonView.getTag();
////                        if (position!=null){
////                            listdata.get(position).ischecked = isChecked;
////                            mAdapter.notifyDataSetChanged();
////                        }
////                        break;
//                    default:
//                        break;
//                }
//            }
//        });
//        checkbox_shopcar_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                Integer position = (Integer) buttonView.getTag();
//                if (position != null) {
//                    listdata.get(position).ischecked = isChecked;
//                    mAdapter.notifyDataSetChanged();
//                }
//            }
//        });
//
//
//        buttondelete_shopcar_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Integer position = (Integer) view.getTag();
//                if (position != null) {
//                    listdata.remove((int) position);
//                    mAdapter.notifyDataSetChanged();
//                }
//            }
//        });
//
//        buttondecline_shopcar_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Integer position = (Integer) view.getTag();
//                if (position != null) {
//                    if (listdata.get(position).count == 0) {
//                        return;
//                    }
//                    listdata.get(position).count--;
//                    mAdapter.notifyDataSetChanged();
//                }
//            }
//        });
//
//        buttonadd_shopcar_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Integer position = (Integer) view.getTag();
//                if (position != null) {
//                    listdata.get(position).count++;
//                    mAdapter.notifyDataSetChanged();
//                }
//            }
//        });

//        button_shopcar_settle.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Integer position = (Integer) view.getTag();
//                if (view!=null){
//                    switch (view.getId()) {
//                        case R.id.buttonadd_shopcar_item:
//                            if (position != null) {
//                                listdata.get(position).count++;
//                                mAdapter.notifyDataSetChanged();
//                            }
//                            break;
//                        case R.id.buttondecline_shopcar_item:
//                            if (position != null) {
//                                if (listdata.get(position).count == 0) {
//                                    return;
//                                }
//                                listdata.get(position).count--;
//                                mAdapter.notifyDataSetChanged();
//                            }
//                            break;
//                        case R.id.buttondelete_shopcar_item:
//                            if (position != null) {
//                                listdata.remove((int) position);
//                                mAdapter.notifyDataSetChanged();
//                            }
//                            break;
//                        default:
//                            break;
//                    }   }
//            }
//        });

        //给adapter设置一个数据改变的监听
        mAdapter.registerDataSetObserver(new DataSetObserver() {
            // 在你 调用Adapter的notifiedDataChange 的时候就调用这个方法
            @Override
            public void onChanged() {
                super.onChanged();
                int money = mAdapter.getMoney();
                textall_shopcar.setText(money + "");
            }
        });
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        listdata = new ArrayList<>();
        //适配器
        mAdapter = new Shopcar_listview_adapter(listdata, getActivity(), new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                switch (buttonView.getId()) {
                    case R.id.checkbox_shopcar_checkall:
                        for (int i = 0; i < listdata.size(); i++) {
                            listdata.get(i).ischecked = isChecked;
                        }
                        mAdapter.notifyDataSetChanged();
                        break;
                    case R.id.checkbox_shopcar_item:
                        Integer position = (Integer) buttonView.getTag();
                        if (position!=null){
                            listdata.get(position).ischecked = isChecked;
                            mAdapter.notifyDataSetChanged();
                        }
                        break;
                    default:
                        break;
                }
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer position = (Integer) view.getTag();
                if (view!=null){
                    switch (view.getId()) {
                        case R.id.buttonadd_shopcar_item:
                            if (position != null) {
                                listdata.get(position).count++;
                                mAdapter.notifyDataSetChanged();
                            }
                            break;
                        case R.id.buttondecline_shopcar_item:
                            if (position != null) {
                                if (listdata.get(position).count == 0) {
                                    return;
                                }
                                listdata.get(position).count--;
                                mAdapter.notifyDataSetChanged();
                            }
                            break;
                        case R.id.buttondelete_shopcar_item:
                            if (position != null) {
                                listdata.remove((int) position);
                                mAdapter.notifyDataSetChanged();
                            }
                            break;
                        default:
                            break;
                    }   }
            }
        });
        //数据源
        adapter = new voersear_listview_adapter(null, null);
        List<Shopcar_list_handler> getdata = adapter.getdata();
        if (getdata != null && getdata.size() > 0) {
            listdata.addAll(getdata);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.listview_fragment_shopcar);
        textall_shopcar = (TextView) view.findViewById(R.id.text_shopcar_money);
        mCheckBoxall = (CheckBox) view.findViewById(R.id.checkbox_shopcar_checkall);
        button_shopcar_settle = (Button) view.findViewById(R.id.button_shopcar_settle);
        checkbox_shopcar_item = (CheckBox) view1.findViewById(R.id.checkbox_shopcar_item);
        buttonadd_shopcar_item = (Button) view1.findViewById(R.id.buttonadd_shopcar_item);
        buttondecline_shopcar_item = (Button) view1.findViewById(R.id.buttondecline_shopcar_item);
        buttondelete_shopcar_item = (Button) view1.findViewById(R.id.buttondelete_shopcar_item);
    }
}
